<?php

namespace app\common\cachemodel\zl6;

use app\common\model\BaseCache;
use XznsTools\cache\CacheManage;

class Zl6MeditationSubscribe extends BaseCache
{
    protected $table = 'wechat_backup_07_18_14_43_00.zl6_meditation_subscribe';

    protected $cacheModel = 'meditation.subscribe';

    /**
     * 订阅信息
     * @var null | array
     */
    protected $subscribeData = null;

    /**
     * 写入 订阅信息
     * @param int $uid
     * @param array $data
     * @return int
     */
    public function SaveData(int $uid, array $data)
    {
        try
        {
            $id = $this->insertGetId($data);

            $this->listDataAdd($uid, [$data['mid'] => $id]);
            return (int)$id;
        }
        catch (\Exception $e)
        {
            \think\Log::write(__CLASS__ . ' => ' . __FUNCTION__ . '()异常：' . $e->getMessage() . '失败信息：' . $e->getFile() . ':' . $e->getLine(), 'error', true);
            return 0;
        }
    }

    public function UpdateData(int $id, array $data)
    {
        try
        {
            $this->where(['id' => $id])->update($data);
            return true;
        }
        catch (\Exception $e)
        {
            \think\Log::write(__CLASS__ . ' => ' . __FUNCTION__ . '()异常：' . $e->getMessage() . '失败信息：' . $e->getFile() . ':' . $e->getLine(), 'error', true);
            return false;
        }
    }

    /**
     * 取消订阅
     * @param int $id
     * @param int $mid
     * @param int $uid
     * @return bool
     */
    public function delData(int $id, int $mid, int $uid)
    {
        try
        {
            $this->where(['id' => $id])->delete();

            $this->listDataDel($uid, [$mid]);
            return true;
        }
        catch (\Exception $e)
        {
            \think\Log::write(__CLASS__ . ' => ' . __FUNCTION__ . '()异常：' . $e->getMessage() . '失败信息：' . $e->getFile() . ':' . $e->getLine(), 'error', true);
            return false;
        }
    }

    /**
     * 获取用户 解锁课程信息
     * @param int $uid
     * @return array
     * @throws \Exception
     */
    public function getSubscribeMidsFromCache(int $uid)
    {
        return CacheManage::getInstance()->setCacheConfig($this->cacheModel, $uid)->ZsGetIocData('list', function () use ($uid)
        {
            return $this->getSubscribeMidsFromDms($uid);
        }, true);
    }

    /**
     * 数据库获取不同类型的推荐id
     * @param $uid
     * @return array
     */
    public function getSubscribeMidsFromDms($uid)
    {
        try
        {
            $data = $this->where(['uid' => $uid])->order('id', 'DESC')->column('mid', 'id');

            $resData = [0 => 0];
            foreach ($data as $id => $mid)
            {
                $resData[$mid] = $id;
            }

            return $resData;
        }
        catch (\Exception $e)
        {
            return [];
        }
    }

    /**
     * 批量获取地图解锁结果信息
     * @param $ids
     * @param $type
     * @param $sortType
     * @return array|false|string|void
     */
    public function getInfoByIdsInData($ids, $type, $sortType)
    {
        try
        {
            $data = $this->where(['id' => ['in', $ids]])->column('uid,mid,is_send,update_time,create_time', 'id');
        }
        catch (\Exception $e)
        {
            $data = [];
        }
        return $data;
    }

    public function batchGetInfoByIds($cids, $field = [])
    {
        $resData = parent::batchGetInfoByIds($cids, $field);

        return $this->dataInit($resData);
    }

    /**
     * 数据初始化
     * @param array $data
     * @return array
     */
    public function dataInit(array $data)
    {
        foreach ($data as $index => $itemData)
        {
            $data[$index] = arrayKeyData2int($itemData, ['id', 'uid', 'mid', 'is_send', 'update_time', 'create_time']);
        }

        return $data;
    }

    /**
     * 已订阅信息
     * @param int $uid
     * @return array
     * @throws \Exception
     */
    public function getSubscribeDatas(int $uid)
    {
        if ($this->subscribeData === null)
        {
            $this->subscribeData = $this->getSubscribeMidsFromCache($uid);
        }

        return $this->subscribeData;
    }

    /**
     * 是否已订阅
     * @param int $uid
     * @param int $mid
     * @return int
     * @throws \Exception
     */
    public function isSubscribe(int $uid, int $mid)
    {
        $subscribeData = $this->getSubscribeDatas($uid);
        $id = $subscribeData[$mid] ?? 0;
        return (int)round($id);
    }
}